package com.sugon.controller.module;

import com.sugon.entity.*;
import com.sugon.service.*;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.sugon.utils.ShiroUtils.getUserId;

/**
 * 模具资料--附件关联Controller
 *
 * @author sugon
 * @date 2019-07-31 14:38:51
 */
@Controller
@RequestMapping("moduledatafile")
public class ModuleDataFileController {
    @Autowired
    private ModuleDataFileService moduleDataFileService;
    @Autowired
    private AnnexFileService annexFileService;
    @Autowired
    private KnowBaseService knowBaseService;
    @Autowired
    private ModuleDataService moduleDataService;
    @Autowired
    private KnowFileService knowFileService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("moduledatafile:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<ModuleDataFileEntity> moduleDataFileList = moduleDataFileService.queryList(query);
        int total = moduleDataFileService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(moduleDataFileList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("moduledatafile:info")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        ModuleDataFileEntity moduleDataFile = moduleDataFileService.queryObject(id);

        return R.ok().put("moduleDataFile", moduleDataFile);
    }

    /**
     * 保存
     */
    @RequestMapping("/save/{moduleCode}")
    @RequiresPermissions("moduledatafile:save")
    @ResponseBody
    public R save(@RequestBody AnnexFileEntity annexFile, @PathVariable("moduleCode") String moduleCode) {
        annexFile.setUploadDate(new Date());
        annexFileService.save(annexFile);
        ModuleDataFileEntity moduleDataFile = new ModuleDataFileEntity();
        moduleDataFile.setFileId(annexFile.getId());
        moduleDataFile.setModuleArchiveCode(moduleCode);
        moduleDataFileService.save(moduleDataFile);

        //纳入知识库的情况
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> map1 = new HashMap<>();
        map.put("moduleCode", moduleCode);
        map1.put("devCode", moduleCode);
        List<ModuleDataEntity> list = moduleDataService.queryList(map);
        if (list != null && list.size() != 0) {
            ModuleDataEntity moduleData = list.get(0);
            if (moduleData.getKnowledge().equals(1)) {
                List<KnowBaseEntity> knowList = knowBaseService.queryList(map1);
                KnowFileEntity knowFile = new KnowFileEntity();
                knowFile.setFileId(annexFile.getId());
                knowFile.setKnowId(knowList.get(0).getId());
                knowFile.setType("2");//模具
                knowFileService.save(knowFile);
            }
        }
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("moduledatafile:update")
    @ResponseBody
    public R update(@RequestBody ModuleDataFileEntity moduleDataFile) {
        moduleDataFile.setUpdateUser(getUserId());
        moduleDataFile.setUpdateTime(new Date());
        moduleDataFileService.update(moduleDataFile);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("moduledatafile:delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        moduleDataFileService.deleteBatch(ids);

        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<ModuleDataFileEntity> list = moduleDataFileService.queryList(params);

        return R.ok().put("list", list);
    }
}
